/*
 * @Queue.java   1.0 2008/07/28
 *
 * Copyright 2008 Web & Zen Multimedia, All rights reserved.
 * Use is subject to license terms.
 */
package it.wzm.lightweightlog;

import java.util.LinkedList;
import java.util.Properties;

/**
 * This class is the place where the Entries are stored, by default a Queue is cumulable
 * which means we want to first store all entries then a {@link QueueDumper} is subsequently called
 * to empty and unload each stored Entry to registered Dumper.
 * @author Leonardo Celati
 * @param <E> any extended Entry
 */
abstract class Queue<E extends Entry> extends LightweightlogObject {
    
    /**
	 * As required by serialization
	 */
	private static final long serialVersionUID = 5840115385777623780L;
	/**
     * true if this Queue is cumulable
     */
    private boolean cumulable;
    /**
     * Default constructor
     * @param p custom set of properties
     */
    public Queue(Properties p) {
        // assignment with defaul name of queue...
        super("queue",p);
        this.setStatus(Status.INIT);

    	// deciding if we are cumulable or not...
        try { 
            this.cumulable=Boolean.parseBoolean(this.get("cumulable").toLowerCase() );            
        } catch (Exception ex) {
            this.cumulable=false;
        }
        // final status...
        this.setStatus(Status.RUNNING);
    }
   
    /**
     * Inform us if this Queue is Cumulable or not. Default to true
     * @return true or false
     */
    public final boolean isCumulable() {
        return cumulable;
    }
    
    /**
     * See if this cell is empty
     * @return true if it's empty, false if not
     */
    public abstract boolean isEmpty();
    
    /**
     * Store the element into the Queue
     * @param e Any extended Entry
     * @return true if the element is correctly added to queue
     * @throws QueueException
     */
    abstract boolean push(E e)  throws QueueException;
    
    /**
     * Get last E from Queue 
     * @return E
     * @throws QueueException
     */
    abstract E pop() throws QueueException;
        
    /**
     * Retrieve the actual size of this Queue
     * @return the size of this Queue
     */
    public abstract int size();
    
    /**
     * Retain all Entry in queue
     * @return LinkedList of E
     */
    abstract LinkedList<E> unload() throws QueueException;
    
    /**
     * Remove all Entry starting until given index
     * @param index
     * @return LinkedList of E
     */
    abstract LinkedList<E> unload(int index) throws QueueException;
    
    /**
     * Overwriting the inherited method from Object
     * @return Valid string
     */
    @Override
    public String toString() {
        //  returning...
        return super.toString() + " {cumulable=" + this.cumulable + ",size=" + this.size() + "}" ;
    }
    
}